<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;

class Orders extends BaseController
{
    // 我的订单
    public function myorders()
    {
        $user = $this->app->session->get('user');
        // $data['lists'] = Db::table('orders')->where('uid', $user['id'])->order('id', 'desc')->select();
        // $orders_products = Db::table('orders_product')->whereIn('ord_no', $data['lists']->column('ord_no'))->select();
        // $product_skus = Db::table('product_sku')->whereIn('out_no', $orders_products->column('sku_out_no'))->select();
        // $data['lists'] = $data['lists']->map(function ($item) use ($product_skus, $orders_products) {
        //     $item['products'] = $orders_products->filter(function ($el) use ($item) {
        //         return $el['ord_no'] == $item['ord_no'];
        //     });
        //     $item['products'] = $item['products']->map(function($ele) use ($product_skus) {
        //         $ele['sku'] = $product_skus->filter(function($elem) use ($ele) {
        //             return $elem['out_no'] == $ele['sku_out_no'];
        //         })->shift();
        //         return $ele;
        //     });
        //     $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
        //     $status_text = [0 => '<span style="color:red">未付款</span>', 1 => '<span style="color:green">已付款</span>', 2 => '已收货', 3 => '已结束'];
        //     $item['status_text'] = $status_text[$item['status']];
        //     return $item;
        // });

        // $data['lists'] = Db::table('orders')->where('uid', $user['id'])->order('id', 'desc')->select()->toArray();
        // $orders_products = Db::table('orders_product')->whereIn('ord_no', array_column($data['lists'], 'ord_no'))->select()->toArray();
        // $product_skus = Db::table('product_sku')->whereIn('out_no', array_column($orders_products, 'sku_out_no'))->select()->toArray();
        // foreach($data['lists'] as $key => $list) {
        //     $products = array_filter($orders_products, function($elem) use ($list) {
        //         return $elem['ord_no'] == $list['ord_no'];
        //     });
        //     foreach($products as $k => $product) {
        //         $sku = current(array_filter($product_skus, function($item) use ($product) {
        //             return $item['out_no'] == $product['sku_out_no'];
        //         }));
        //         $products[$k]['sku'] = $sku;
        //     }
        //     $data['lists'][$key]['products'] = $products;
        //     $data['lists'][$key]['add_time'] = date('Y-m-d H:i:s', $data['lists'][$key]['add_time']);
        //     $status_text = [0 => '<span style="color:red">未付款</span>', 1 => '<span style="color:green">已付款</span>', 2 => '已收货', 3 => '已结束'];
        //     $data['lists'][$key]['status_text'] = $status_text[$data['lists'][$key]['status']];
        // }

        // $data['lists'] = Db::table('orders')->where('uid', $user['id'])->order('id', 'desc')->select()->toArray();
        // $orders_products = Db::table('orders_product')->whereIn('ord_no', array_column($data['lists'], 'ord_no'))->select()->toArray();
        // $product_skus = Db::table('product_sku')->whereIn('out_no', array_column($orders_products, 'sku_out_no'))->select()->toArray();
        // $data['lists'] = array_map(function($list) use ($orders_products, $product_skus) {
        //     $products = array_filter($orders_products, function($elem) use ($list) {
        //         return $elem['ord_no'] == $list['ord_no'];
        //     });
        //     $products = array_map(function($product) use ($product_skus) {
        //         $product['sku'] = current(array_filter($product_skus, function($item) use ($product) {
        //             return $item['out_no'] == $product['sku_out_no'];
        //         }));
        //         return $product;
        //     }, $products);
        //     $list['products'] = $products;
        //     $list['add_time'] = date('Y-m-d H:i:s', $list['add_time']);
        //     $status_text = [0 => '<span style="color:red">未付款</span>', 1 => '<span style="color:green">已付款</span>', 2 => '已收货', 3 => '已结束'];
        //     $list['status_text'] = $status_text[$list['status']];
        //     return $list;
        // }, $data['lists']);

        $data['page'] = max(1, (int)input('get.page'));
        $data['pageSize'] = (int)input('get.limit', 5);
        $data['lists'] = Db::table('orders')->where('uid', $user['id'])->order('id', 'desc')->paginate($data['pageSize']);
        $data['total'] = $data['lists']->total();
        $orders_products = Db::table('orders_product')->whereIn('ord_no', $data['lists']->column('ord_no'))->select();
        $product_skus = Db::table('product_sku')->whereIn('out_no', $orders_products->column('sku_out_no'))->select();
        $data['lists'] = $data['lists']->map(function ($item) use ($product_skus, $orders_products) {
            $item['products'] = $orders_products->filter(function ($el) use ($item) {
                return $el['ord_no'] == $item['ord_no'];
            });
            $item['products'] = $item['products']->map(function($ele) use ($product_skus) {
                $ele['sku'] = $product_skus->filter(function($elem) use ($ele) {
                    return $elem['out_no'] == $ele['sku_out_no'];
                })->shift();
                return $ele;
            });
            $item['add_time'] = date('Y-m-d H:i:s', $item['add_time']);
            $status_text = [0 => '<span style="color:red">未付款</span>', 1 => '<span style="color:green">已付款</span>', 2 => '已收货', 3 => '已结束'];
            $item['status_text'] = $status_text[$item['status']];
            return $item;
        });

        return view('/orders/myorders', $data);
    }

    // 删除订单
    public function del() {
        $user = $this->app->session->get('user');
        $id = (int)input('post.id');
        $item = Db::table('orders')->where('uid', $user['id'])->where('id', $id)->find();
        !$item && exit(json_encode(['code' => 1, 'msg' => '数据错误']));
        $item['status'] != 3 && exit(json_encode(['code' => 1, 'msg' => '订单未完成']));
        Db::table('orders')->where('id', $item['id'])->delete();
        Db::table('orders_product')->whereIn('ord_no', $item['ord_no'])->delete();
        exit(json_encode(['code' => 0, 'msg' => '删除成功']));
    }
}
